/* eslint-disable vue/multi-word-component-names */
<template>
  <div class="login-container">
    <div class="login-form">
      <div class="logo-container">
        <img src="../assets/航空工业.jpeg" alt="logo" class="logo-image">
        <span class="logo-text">直升机结构装配前摄性动态排产软件</span>
      </div>
      <el-form :model="loginForm" class="form-content">
        <el-form-item>
          <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      }
    };
  },
  methods: {
    login() {
      axios.post('http://localhost:8000/api/login/login', {
        username: this.loginForm.username, // 使用 loginForm 里的数据
        password: this.loginForm.password
      }).then(response => {
        const token = response.data.token;
        // 将 token 保存到 localStorage
        localStorage.setItem('token', response.data.token);
        console.log(localStorage.getItem('token'));
         // 设置全局的 axios 请求头
    axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
    // 触发 'login-success' 事件
    this.$emit('login-success');
      }).catch(error => {
        console.error('登录失败', error);
      });
    }
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
  width: 110%;
  background-image: url('../assets/昌飞.jpg'); /* 替换为你的背景图片路径 */
  background-size: cover; /* 背景图片覆盖整个容器 */
  background-position: center; /* 背景图片居中显示 */
  background-repeat: no-repeat; /* 不重复背景图片 */
}

.login-form {
  width: 450px;
  padding: 20px;
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.logo-container {
  margin-bottom: 20px; /* 图标和文字与登录框的间距 */
  text-align: center; /* 确保文字居中 */
}

.logo-image {
  height: 100px; /* 调整图标高度 */
}

.logo-text {
  display: block;
  margin-top: 15px; /* 图标和文字的间距 */
  font-size: 25px;
  font-weight: bold;
  color: #02478e;
}

.el-button {
  background-color:#02478e;
}
</style>
